﻿@{
    ViewBag.Title = "Form"; 
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    layui.use(['jquery', 'form', 'laydate', 'commonTable', 'table', 'common', 'optimizeSelectOption'], function () {
        var form = layui.form,
            $ = layui.$,
            laydate = layui.laydate,
            table = layui.table,
            commonTable = layui.commonTable,
            common = layui.common;
        var keyValue = $.request("keyValue");
        //执行一个laydate实例
        laydate.render({
            elem: '#F_PlanTime'
            , btns: ['clear']
            , trigger: 'click',
            format: 'yyyy/MM/dd HH:mm:ss',
        });
        //权限字段
        common.authorizeFields('adminform');
        var listData = [];
        $(function () {
            initControl();
            if (!!keyValue) {
                common.ajax({
                    url: '/MaterialManage/InStorage/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        if (!!data.F_BatchJson) {
                            $('#F_BandingMark').attr("checked", "checked");
                            listData = JSON.parse(data.F_BatchJson);
                        }
                        else {
                            $('#F_BandingMark').removeAttr("checked");
                        }

                    }
                });
            }
            else {
                var code = new Date().formatDate('yyyyMMddhhmmss');
                $('#F_InStorageCode').val("IN-" + code);
            }
            form.render();
            commonTable.rendertable({
                elem: '#currentTableId',
                data: listData,
                defaultToolbar: [],
                search: false,
                limit: 9999,//每页数据 默认
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count'] //自定义分页布局
                    , first: false //不显示首页
                    , last: false //不显示尾页
                },
                height: 'full-265',
                filter: {
                    clearFilter: false,
                },
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'F_MaterialBatch', title: '批号', edit: 'text', minWidth: 145, sort: true },
                    { field: 'F_Num', title: '数量', width: 200, edit: 'text', sort: true },
                ]]
            });
            //行点击事件监听，控制按钮显示
            var oneList = [];//选择1条显示
            var moreList = ["NF-delete"];//多条显示
            commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", oneList, moreList);
        });      
        wcLoading.close();
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data;
            if (obj.event === 'add') {  // 监听添加操作
                common.modalOpen({
                    title: "添加",
                    url: "/MaterialManage/InStorage/AddForm",
                    width: "400px",
                    height: "400px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                var index = parent.layer.load(0, {
                    shade: [0.5, '#000'], //0.1透明度的背景
                });
                var cacheData = table.cache.currentTableId;
                for (var i = 0; i < data.length; i++) {
                    for (var j = 0; j < cacheData.length; j++) {
                        if (data[i].F_Id == cacheData[j].F_Id) {
                            cacheData.splice(j, 1);
                            break;
                        }
                    }
                }
                common.modalMsg("操作成功", "success");
                table.reload('currentTableId', {
                    data: cacheData
                });
                parent.layer.close(index);
            }
            return false;
        });
        function initControl() {
            //此处需修改
            //绑定数据源
            //类型为下拉框时
        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
            postData.list = table.cache.currentTableId;
            if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
            if (!postData["F_BandingMark"]) postData["F_BandingMark"] = false;
            if (!!postData.F_BandingMark) {
                if (!postData.list) {
                    common.modalMsg("未添加批号", "warning");
                    return false;
                }
                var num = 0;
                for (var i = 0; i < postData.list.length; i++) {
                    num = num + Number(postData.list[i].F_Num);
                }
                if (num != Number(postData.F_Num)) {
                    common.modalMsg("批号数量与入库单数量不符", "warning");
                    return false;
                }
            }
            else {
                postData.list = null;
            }
            common.submitForm({
                url: '/MaterialManage/InStorage/SubmitForm?keyValue=' + keyValue,
                param: postData,
                success: function () {
                    common.parentreload('data-search-btn');
                }
            })
            return false;
        });
    });
    function search() {
        layui.use(['jquery', 'form', 'common'], function () {
            var $ = layui.$,
                common = layui.common;
            var parentform = window.name;
            common.modalOpen({
                title: "选择物料",
                url: "/MaterialManage/Material/AddForm?parentform=" + parentform + "&name=" + "F_MaterialName" + "&value=" + "F_MaterialId" + "&ids=" + $('#F_MaterialId').val(),
                width: "650px",
                height: "600px",
            });
        });
    }
</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item">
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">入库单编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_InStorageCode" name="F_InStorageCode" autocomplete="off" maxlength="50" lay-verify="required" placeholder="请输入入库单编号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">物料</label>
                        <div class="layui-input-block">
                            <input id="F_MaterialName" name="F_MaterialName" type="text" lay-verify="required" maxlength="50" autocomplete="off" class="layui-input" onclick="search()" />
                            <input id="F_MaterialId" name="F_MaterialId" type="text" hidden />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label required">数量</label>
                        <div class="layui-input-block">
                            <input type="number" pattern="[0-9]*" id="F_Num" name="F_Num" lay-verify="required|number" oninput="if(value.length>8)value=value.slice(0,8)" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label">计划时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_PlanTime" name="F_PlanTime" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label">车辆</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_CarNo" name="F_CarNo" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-hide">
                        <label class="layui-form-label">选项</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="F_EnabledMark" id="F_EnabledMark" checked="" value="true" title="有效标识">
                            @*<input type="checkbox" name="F_BandingMark" id="F_BandingMark" checked="" value="true" title="预绑定标识">*@
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md12 layui-col-xs12 layui-hide">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <input id="F_Description" name="F_Description" class="layui-input" placeholder="请输入备注">
                        </div>
                    </div>
                </div>
                @*<div class="layui-form-item">
            <div class="layui-col-md12 layui-col-sm12">
                <label class="layui-form-label">批号列表</label>
                <div class="layui-input-block">
                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
            </div>
        </div>*@
                <div class="layui-form-item text-right layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                    <button id="NF-add" name="NF-add" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe642;</i>添加</button>
                    <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger layui-hide" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
                </div>
            </script>
        </div>
    </div>
</body>

